package day8.homeworkII.Task6;

import java.util.*;

public class ServletImpl implements Servlet {

    @Override
    public Set<Card> prepareCard() {
        Set<Card> set = new HashSet<>();

        set.add(new Card("大鬼", "", 15));
        set.add(new Card("小鬼", "", 14));
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 13; j++) {
                String color = "";
                if (i < 2) {
                    color = i == 0 ? "黑桃" : "红桃";
                } else {
                    color = i == 2 ? "梅花" : "方块";
                }
                String value = "";
                if (j == 1) value = "A";
                else if (j == 11) value = "J";
                else if (j == 12) value = "Q";
                else if (j == 0) value = "K";
                else value = String.valueOf(j);

                set.add(new Card(color, value, j == 0 ? 13 : j));
            }
        }
        return set;
    }

    @Override
    public ArrayList<Set<Card>> Licensing(Set<Card> set) {
        ArrayList<Set<Card>> user1 = new ArrayList<Set<Card>>();
        user1.add(new HashSet<Card>());
        user1.add(new HashSet<Card>());
        user1.add(new HashSet<Card>());
        user1.add(new HashSet<Card>());

        List<Card> user2 = new ArrayList<>();

        user2.addAll(set);
        Collections.shuffle(user2);
        for (int i = 0; i < user2.size(); i++){
            if (i < 17) {
                user1.get(0).add(user2.get(i));
            } else if (i < 35 && i > 17) {
                user1.get(1).add(user2.get(i));
            } else if (i > 35 && i < 53) {
                user1.get(2).add(user2.get(i));
            } else {
                user1.get(3).add(user2.get(i));
            }
        }
        return user1;
    }

    @Override
    public ArrayList<StringBuilder> SortCard(ArrayList<Set<Card>> user) {

        List<Card> user1 = new ArrayList<Card>(user.get(0));
        List<Card> user2 = new ArrayList<Card>(user.get(1));
        List<Card> user3 = new ArrayList<Card>(user.get(2));
        List<Card> user4 = new ArrayList<Card>(user.get(3));

        Collections.sort(user1);
        Collections.sort(user2);
        Collections.sort(user3);
        Collections.sort(user4);

        ArrayList<StringBuilder> Cards = new ArrayList<StringBuilder>();

        Cards.add(new StringBuilder("玩家1的手牌为："));
        Cards.add(new StringBuilder("玩家2的手牌为："));
        Cards.add(new StringBuilder("玩家3的手牌为："));
        Cards.add(new StringBuilder("底牌为："));


        for (Card card : user1) {
            Cards.get(0).append(card.toString());
        }

        for (Card card : user2) {
            Cards.get(1).append(card.toString());
        }

        for (Card card : user3) {
            Cards.get(2).append(card.toString());
        }

        for (Card card : user4) {
            Cards.get(3).append(card.toString());
        }

        return Cards;
    }
}
